package vip.xiaonuo.supplier.modular.warehouse.storages.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.warehouse.storages.entity.SupplierStorages;
import vip.xiaonuo.supplier.modular.warehouse.storages.mapper.SupplierStoragesMapper;
import vip.xiaonuo.supplier.modular.warehouse.storages.param.SupplierStoragesAddParam;
import vip.xiaonuo.supplier.modular.warehouse.storages.param.SupplierStoragesEditParam;
import vip.xiaonuo.supplier.modular.warehouse.storages.param.SupplierStoragesIdParam;
import vip.xiaonuo.supplier.modular.warehouse.storages.param.SupplierStoragesPageParam;
import vip.xiaonuo.supplier.modular.warehouse.storages.service.SupplierStoragesService;

import java.util.List;

/**
 * 仓库信息Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 19:51
 **/
@Service
public class SupplierStoragesServiceImpl extends ServiceImpl<SupplierStoragesMapper, SupplierStorages> implements SupplierStoragesService {

    @Override
    public Page<SupplierStorages> page(SupplierStoragesPageParam supplierStoragesPageParam) {
        QueryWrapper<SupplierStorages> queryWrapper = new QueryWrapper<SupplierStorages>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierStoragesPageParam.getParentId())) {
            queryWrapper.lambda().eq(SupplierStorages::getParentId, supplierStoragesPageParam.getParentId());
        }
        if(ObjectUtil.isNotEmpty(supplierStoragesPageParam.getStoreName())) {
            queryWrapper.lambda().like(SupplierStorages::getStoreName, supplierStoragesPageParam.getStoreName());
        }
        if(ObjectUtil.isNotEmpty(supplierStoragesPageParam.getStoreAddress())) {
            queryWrapper.lambda().like(SupplierStorages::getStoreAddress, supplierStoragesPageParam.getStoreAddress());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierStorages::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        //仅查询仓库
        queryWrapper.lambda().eq(SupplierStorages::getStoreType,"STORAGE");
        if(ObjectUtil.isAllNotEmpty(supplierStoragesPageParam.getSortField(), supplierStoragesPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierStoragesPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierStoragesPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierStoragesPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierStorages::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierStoragesAddParam supplierStoragesAddParam) {
        SupplierStorages supplierStorages = BeanUtil.toBean(supplierStoragesAddParam, SupplierStorages.class);
        //数据归属
        supplierStorages.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        supplierStorages.setStoreType("STORAGE");
        this.save(supplierStorages);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierStoragesEditParam supplierStoragesEditParam) {
        SupplierStorages supplierStorages = this.queryEntity(supplierStoragesEditParam.getId());
        BeanUtil.copyProperties(supplierStoragesEditParam, supplierStorages);
        this.updateById(supplierStorages);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierStoragesIdParam> supplierStoragesIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierStoragesIdParamList, SupplierStoragesIdParam::getId));
    }

    @Override
    public SupplierStorages detail(SupplierStoragesIdParam supplierStoragesIdParam) {
        return this.queryEntity(supplierStoragesIdParam.getId());
    }

    @Override
    public SupplierStorages queryEntity(String id) {
        SupplierStorages supplierStorages = this.getById(id);
        if(ObjectUtil.isEmpty(supplierStorages)) {
            throw new CommonException("仓库信息不存在，id值为：{}", id);
        }
        return supplierStorages;
    }
}
